package ch.fhnw.qualiscan.application.pages.tabs;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.pages.NewSurvey;
import ch.fhnw.qualiscan.core.db.helpers.addUserToSurveyArgs;
import ch.fhnw.qualiscan.core.db.pojos.User;

import com.vaadin.terminal.UserError;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class NewSurveyTab3 extends VerticalLayout {
	private NewSurvey newSurvey;
	private HorizontalLayout buttonBar;
	
	public NewSurveyTab3(NewSurvey newSurvey) {
		setMargin(true);
		this.newSurvey = newSurvey;

    	newSurvey.addParticipantsForm = new Form();
    	buildAddParticipantsForm();
    	this.addComponent(newSurvey.addParticipantsForm);

	}
	
	/**
	 * Creates a form containing a multi-line TextField for each Role. Each email-address must be entered on a new line.
	 * In the ClickListner event of the button the email addresses are added to the participantsRoleList
	 */
	private void buildAddParticipantsForm() {
		//newSurvey.addParticipantsForm.setCaption(TM.get("newsurvey.tab3.general.title"));
		newSurvey.addParticipantsForm.setDescription(TM.get("newsurvey.tab3.general.desc"));

		newSurvey.roleList = newSurvey.roleModel.getParticipantRoles();
		
		newSurvey.participantsRole = new TextField[newSurvey.roleList.size()];
    	for(int i = 0; i < newSurvey.roleList.size(); i++) {
    		newSurvey.participantsRole[i] = new TextField();
    		newSurvey.participantsRole[i].setInputPrompt("test@test.com\ntest@test.com\ntest@test.com\ntest@test.com");
    		newSurvey.participantsRole[i].setRows(8);
    		newSurvey.participantsRole[i].setColumns(25);
    		newSurvey.participantsRole[i].setCaption(newSurvey.userModel.getRoleText(newSurvey.roleList.get(i), newSurvey.userSession.getLanguage()).getTitle());
    		newSurvey.addParticipantsForm.addField(newSurvey.roleList.get(i).getId(), newSurvey.participantsRole[i]);
    	}
    	
    	buttonBar = new HorizontalLayout();
    	newSurvey.addParticipantsForm.getFooter().addComponent(buttonBar);
    	
    	// next button
        Button commit = new Button(TM.get("action.next"), new ClickListener() {
			public void buttonClick(ClickEvent event) {
				newSurvey.participantsRoleList = new ArrayList<addUserToSurveyArgs>();
				newSurvey.addParticipantsForm.commit();
				int error = 0;
				Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
				for(int i = 0; i < newSurvey.roleList.size(); i++) {
					String participants = (String)newSurvey.participantsRole[i].getValue();
					if (participants != null) {
						String[] participantsSplited = participants.split("\\n");
						for(int u = 0; u < participantsSplited.length; u++) {
							if (participantsSplited[u] != "") {
								Matcher m = p.matcher(participantsSplited[u]);
								if (m.matches()) {
									User user = newSurvey.userModel.addUser(participantsSplited[u]);
									addUserToSurveyArgs aua = new addUserToSurveyArgs(user, newSurvey.roleList.get(i));
									newSurvey.participantsRoleList.add(aua);
								}
								else {
									error = 1;
								}
							}
						}
					}	
				}
				
				if(error == 0) {
					newSurvey.tab4.setEnabled(true);
					newSurvey.tabSheet.setSelectedTab(newSurvey.TabComponent4);
					newSurvey.tab3.setEnabled(false);
				}
				else {
					//At least one email address is invalid
					newSurvey.addParticipantsForm.setComponentError(new UserError(TM.get("newsurvey.tab3.form.invalidemail")));
					Button commitWithoutCor = new Button(TM.get("action.nextwithoutchange"), new ClickListener() {
						public void buttonClick(ClickEvent event) {
							newSurvey.tab4.setEnabled(true);
							newSurvey.tabSheet.setSelectedTab(newSurvey.TabComponent4);
							newSurvey.tab3.setEnabled(false);
						}
					});
					buttonBar.addComponent(commitWithoutCor);
				}
			}
		});
        commit.setIcon(Config.ICON_GO_TAB);
        
        buttonBar.addComponent(commit);
		
	}
}
